/*
 * @Author: ztao
 * @Date: 2022-04-20 22:38:18
 * @LastEditTime: 2022-04-20 23:04:53
 * @Description: 时间最短,内存消耗最少解法
 */
var romanToInt = function(s) {
  let obj= {
      I: 1,
      V: 5,
      X:10,
      L:50,
      C:100,
      D:500,
      M:1000
  }
  let sArr = s.split('');
  let sum =0;
  for(let i=0; i<sArr.length;i++){
      let item = sArr[i];
      sum += obj[item];
  }
  if(s.indexOf('IV')>-1 || s.indexOf('IX')>-1){
      sum -=2;
  }
  if(s.indexOf('XL')>-1 || s.indexOf('XC')>-1){
      sum -=20;
  }
  if(s.indexOf('CD')>-1 || s.indexOf('CM')>-1){
      sum -=200;
  }
  return sum;
};

//这个解法比较占内存,时间也比较长
var romanToInt1 = function (s) {
  let map = new Map([
      ['I',1],
      ['V',5],
      ['X',10],
      ['L',50],
      ['C',100],
      ['D',500],
      ['M',1000],
  ]);
  let sArr = s.split('');
  let sum =0;
  for(let i=0; i<sArr.length;i++){
    let item = sArr[i];
    sum += map.get(item);
  }
  if(s.indexOf('IV')>-1 || s.indexOf('IX')>-1){
      sum -=2;
  }
  if(s.indexOf('XL')>-1 || s.indexOf('XC')>-1){
      sum -=20;
  }
  if(s.indexOf('CD')>-1 || s.indexOf('CM')>-1){
      sum -=200;
  }
  return sum;
};

let result = romanToInt1("MCMXCIV");

console.log(result);